package com.han.fu.api.business.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.han.fu.api.business.model.ao.*;
import com.han.fu.api.business.model.vo.UserInfoVo;
import com.han.fu.api.business.model.vo.UserStatsVo;
import com.han.fu.api.business.model.vo.UserVo;
import com.han.fu.api.business.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 用户接口
 *
 * @author: star
 * @date: 2021-08-05
 */
@RestController
public class UserController {

    @Autowired
    IUserService iUserService;

    /**
     * 用户分页查询
     *
     * @param ao 分页查询条件
     * @return Page<UserVo> 分页数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/user/page")
    public Page<UserVo> queryUserPage(@Validated @RequestBody UserPageAo ao) {
        return iUserService.queryUserPage(ao);
    }

    /**
     * 用户明细查询
     *
     * @param id 主键
     * @return UserInfoVo 明细
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @GetMapping("/user/detail")
    public UserInfoVo queryUserDetail(@NotNull(message = "ID不能为空") @RequestParam String id) {
        return iUserService.queryUserDetail(id);
    }

    /**
     * 用户新增数据
     *
     * @param ao 数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/user/add")
    public void addUser(@Validated @RequestBody UserAddAo ao) {
        iUserService.addUser(ao);
    }

    /**
     * 用户修改数据
     *
     * @param ao 数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/user/update")
    public void updateUser(@Validated @RequestBody UserUpdateAo ao) {
        iUserService.updateUser(ao);
    }

    /**
     * 用户删除数据
     *
     * @param ao 数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/user/delete")
    public void deleteUser(@Validated @RequestBody UserDeleteAo ao) {
        iUserService.deleteUser(ao);
    }

    /**
     * 用户统计查询
     *
     * @param ao 查询条件
     * @return List<UserStatsVo> 统计数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/user/stats")
    public List<UserStatsVo> queryUserStats(@Validated @RequestBody UserStatsAo ao) {
        return iUserService.queryUserStats(ao);
    }

    /**
     * 用户注册
     *
     * @param ao 数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/user/register")
    public void registerUser(@Validated @RequestBody RegisterAo ao) {
        iUserService.registerUser(ao);
    }

    /**
     * 用户修改密码
     *
     * @param ao 数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/user/change/password")
    public void changePasswordUser(@Validated @RequestBody ChangePasswordAo ao) {
        iUserService.changePasswordUser(ao);
    }

}